import { createApp } from 'vue'
import { createRouter, createWebHistory } from  'vue-router'

import App from '@/views/App.vue'

import element from 'element-plus';
import 'element-plus/theme-chalk/index.css'

const requireAll = requireContext => requireContext.keys().map(requireContext)

const routerChildrenContext = require.context('@/views/', true, /router\.js$/)

let routes = []

let rootRouter = {
  path: '/',
  component: App,
  children: []
}

// 重定向路由
let redirectRoute = {
  path: '/:pathMatch(.*)*',
  redirect: '/'
}

const routerChildren = requireAll(routerChildrenContext)

routerChildren.map((route) => {
  route.default(rootRouter.children)
})

const relRoutes = routes.concat([rootRouter, redirectRoute])

const router = createRouter({
	history: createWebHistory(),
	routes: relRoutes
})

createApp(App).use(router).use(element).mount('#app')

export default router